package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.model.TbSeller;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.uitl.ShiroUtils;
import com.dongdongshop.vo.SellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @PostMapping("getSellerByName")
    public Result getSellerByName(@RequestParam String sellerId){
        SellerVo sellerVo=sellerService.getSellerByName(sellerId);
        return Result.OK().setData(sellerVo);
    }

    //查询
    @PostMapping("showSeller")
    public Result SellerList(@RequestHeader String AutharizeToken,@RequestBody SellerVo vo){
        List<SellerVo> voList = sellerService.SellerList(vo);
        return Result.OK().setData(voList);
    }

    //增加
    @PostMapping("addseller")
    public Result addseller(@RequestBody SellerVo vo){
        try {
            //根据sellerId查询数据是否存在
            List<TbSeller> list = sellerService.showbysellerId(vo.getSellerId());
            if (list.size() == 0){
                String s = ShiroUtils.generateSalt(6);
                //需要加密的密码
                String pwd = vo.getPassword();
                //加密方法
                String md5 = ShiroUtils.encryptPassword("MD5", pwd, s, 3);
                vo.setSalt(s);
                vo.setPassword(md5);
                sellerService.addTbSeller(vo);
                return Result.response(ResultEnums.REGISTER_SUCCESS);
            }else {
                return Result.response(ResultEnums.REGISTER_ERRORNAME);
            }
        }catch (Exception e){
            System.out.println(e);
            return Result.response(ResultEnums.REGISTER_ERROR);
        }
    }
    //修改单个商家数据
    @RequestMapping("updateTbSeller")
    public Result updateTbSeller(@RequestBody SellerVo sellerVo) {
        try {
            sellerService.updateTbSeller(sellerVo);
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }
    @RequestMapping("TbSellershowbysellerId")
    public Result TbSellershowbysellerId(@RequestParam String sellerId){
        try {
            SellerVo sellerVo = sellerService.TbSellershowbysellerId(sellerId);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(sellerVo);
        } catch (Exception e) {
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }
}
